﻿using System;
using BPM.Server;
using BPM.Server.Data.Common;
using System.Text;
using yc.bpm.BizValidation;


namespace yc.bpm.validation.oaworkdiarybill
{
    /// <summary>
    /// 工作日志验证
    /// </summary>
    public class Validator
    {
        static yc.bpm.logger.Log _logger = new yc.bpm.logger.Log();

        /// <summary>
        /// 校验耗时百分比
        /// </summary>
        public static void Validate() {

            _logger.Info("yc.bpm.validation.oaworkdiarybill");

            StringBuilder sb = new StringBuilder();
            try
            {
                //验证工作日志耗时百分比
                Validate_CheckWorkDiary(sb);
                if (!string.IsNullOrEmpty(sb.ToString()))
                {
                    throw new ValidationException(sb.ToString());        
                 }
 
                
            }
            catch (ValidationException e)
            {
                //抛出验证错误信息
                throw new Exception(e.Message);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw;
            }
            finally
            {
                //dbContext.Dispose();
            }
        }

        private static void Validate_CheckWorkDiary(StringBuilder sbCheck)
        {
            try
            {
                FlowDataTable tables = Context.Current.FormDataSet.Tables["oa_workdiary_t_detail"];   //子表
                int rowNo = 0;
                string where = string.Empty;
                string datasourceName = "BPMDATA";
                string sqlQuery;
                BPMDBParameterCollection parameters = new BPMDBParameterCollection();
                bool clientCursor = true;
                int startRowIndex = 0;
                int rows = 100;
                int rowcount;
                
                StringBuilder sb=new StringBuilder();
                
                foreach (FlowDataRow bodyRow in tables.Rows)
                {
                    sb.Clear();
                    rowNo++;
                    where = "";
                    string strWriteDate = Convert.ToDateTime(bodyRow["write_date"]).ToString("yyyy-MM-dd");
                    decimal dFormValue = 0;
                    GetBodysSumValue(tables, "write_date", strWriteDate, "percentage", out dFormValue);//Convert.ToDecimal(bodyRow["feeamt"]);       //页面上 金额信息
                    sqlQuery = string.Format(" select sum(isnull(percentage,0)) percentage  from oa_workdiary_t_detail where write_date='{0}'", strWriteDate);
 
                    FlowDataTable flowtable = DataSourceManager.LoadTable(datasourceName, sqlQuery, parameters, clientCursor, startRowIndex, rows, out rowcount);
                    decimal percentage = Convert.ToDecimal(flowtable.Rows[0]["percentage"]);
                    if (dFormValue + percentage > 100)
                    {
                        sb.AppendLine(string.Format("日志明细中{0}的耗时百分比不能大于100%，其中页面的为{1}%，后台的为{2}%"
                            , strWriteDate
                            , dFormValue
                            , percentage
                          ));
                    }
                    if (!string.IsNullOrEmpty(sb.ToString()))
                    {
                        sbCheck.Append(sb.ToString());
                    }
                   
                }

            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw;
            }
            finally {
            }
        }

        /// <summary>
        /// 得到明细表的相同ID的合计数据
        /// </summary>
        /// <param name="tables"></param>
        /// <param name="idColumn"></param>
        /// <param name="idValue"></param>
        /// <param name="sumColumn"></param>
        /// <param name="iReturnValue"></param>
        private static void GetBodysSumValue(FlowDataTable tables, string idColumn, string idValue, string sumColumn, out decimal iReturnValue)
        {
            decimal dFormValue = 0;
            try
            {
                foreach (FlowDataRow bodyRow in tables.Rows)
                {
                    if (Convert.ToDateTime(bodyRow[idColumn]).ToString("yyyy-MM-dd").Equals(idValue))
                    {
                        dFormValue = dFormValue + Convert.ToDecimal(bodyRow[sumColumn]);       //页面上 金额信息
                        _logger.Info("dFormValue:" + dFormValue.ToString());
                    }
                }
                iReturnValue = dFormValue;
            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw;
            }
            finally
            {
                //dbContext.Dispose();
            }
        }

        
    }

    
}
